package com.iot.inter.interfaceprocessing.newinterface;

import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newpojo.TurntoInterfaceUtils;
import com.iot.inter.entity.newpojo.request.RequestBody.SyncModulePackagesRequestBody;
import com.iot.inter.entity.newpojo.request.RequestBody.SyncPackageAndDeviceRequestBody;
import com.iot.inter.entity.newpojo.request.RequestBody.SynchronousPartnerRequestBody;
import com.iot.inter.entity.newpojo.request.RequestHeader;
import com.iot.inter.entity.newpojo.request.subRequest.SyncModulePackagesRequest;
import com.iot.inter.entity.newpojo.request.subRequest.SyncPackageAndDeviceRequest;
import com.iot.inter.entity.newpojo.request.subRequest.SynchronousPartnerRequest;
import com.iot.inter.entity.newpojo.response.subResponse.SyncPackageAndDeviceResponse;
import com.iot.inter.entity.newpojo.response.subResponse.SyncPackagesResponse;
import com.iot.inter.entity.newpojo.response.subResponse.SynchronousPartnerResponse;
import com.iot.inter.utils.HttpClientTool;
import com.iot.inter.utils.JaxbUtil;
import org.apache.log4j.Logger;

/**
 * 实现同步操作的实现类：
 * 1，同步企业信息
 * 2，同步套餐信息
 * 3，同步设备和企业和套餐的对应关系
 *
 * @author lm
 * @date 2019/8/29
 */
public class TrunToSyncService {

    private static Logger logger = Logger.getLogger(TrunToSyncService.class);

    /**
     * 3.17	同步企业信息
     *
     * @author lm
     * @date 2019/8/29
     */
    public SynchronousPartnerResponse syncPartner(Integer pageNo, Integer pageSize) {
        try {

            // 请求参数拼接
            SynchronousPartnerRequestBody body = new SynchronousPartnerRequestBody();
            if (pageNo != null) {
                body.setPageNo(pageNo.toString());
            } else {
                body.setPageNo("1");
            }
            if (pageNo != null) {
                body.setPageSize(pageSize.toString());
            } else {
                body.setPageSize("20");
            }

            SynchronousPartnerRequest request = new SynchronousPartnerRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();

            RequestHeader header = utils.getRequestHeader(Constant.cmd_syncPartner, Constant.cmd_accessCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), Constant.cmd_secret);
            request.setSign(sign);
            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SynchronousPartnerRequest.class).toXml(request, "utf-8");

            // 发送post请求，并接收返回结果

            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("【同步企业信息】xml请求URL=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_syncPartner);

            // 将回传的xml转换成object数据
            SynchronousPartnerResponse responseObj = new JaxbUtil(SynchronousPartnerResponse.class).fromXml(result, "utf-8");

            if (result != null && !result.equals("-1")) {

                responseObj.setBody(responseObj.getBody());
                responseObj.setRetCode(responseObj.getHeader().getResultCode());
                responseObj.setResultInfo(responseObj.getHeader().getResultInfo());
                return responseObj;

            } else {
                SynchronousPartnerResponse response = new SynchronousPartnerResponse();
                response.setRetCode("9998");
                response.setResultInfo("接口返回异常");
                return response;

            }

        } catch (Exception e) {
            SynchronousPartnerResponse response = new SynchronousPartnerResponse();
            response.setRetCode("9999");
            response.setResultInfo("接口出现异常");
            return response;
        }

    }

    /**
     * 3.1	同步资费套餐
     *
     * @author lm
     * @date 2019/8/29
     */
    public SyncPackagesResponse syncPackages(Integer pageNo, Integer pageSize, String partnerCode, String secret) {

        try {
            SyncModulePackagesRequestBody body = new SyncModulePackagesRequestBody();
            if (pageNo != null) {
                body.setPageNo(pageNo.toString());
            } else {
                body.setPageNo("1");
            }
            if (pageNo != null) {
                body.setPageSize(pageSize.toString());
            } else {
                body.setPageSize("20");
            }
            // 请求参数拼接
            SyncModulePackagesRequest request = new SyncModulePackagesRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();

            RequestHeader header = utils.getRequestHeader(Constant.cmd_syncPackages, partnerCode);
            request.setHeader(header);
            request.setBody(body);
            String sign = utils.getSign(header, body, body.getClass(), secret);
            request.setSign(sign);
            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SyncModulePackagesRequest.class).toXml(request, "utf-8");
            logger.info("【同步资费套餐】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("URL=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_syncPackages + ";请求的企业：" + partnerCode);

            logger.info("【同步资费套餐】xml响应：" + result);
            // 将回传的xml转换成object数据
            SyncPackagesResponse responseObj = new JaxbUtil(SyncPackagesResponse.class).fromXml(result, "utf-8");

            if (result != null && !result.equals("-1")) {
                responseObj.setBody(responseObj.getBody());
                responseObj.setRetCode(responseObj.getHeader().getResultCode());
                responseObj.setResultInfo(responseObj.getHeader().getResultInfo());
                return responseObj;

            } else {
                SyncPackagesResponse response = new SyncPackagesResponse();
                response.setRetCode("9998");
                response.setResultInfo("接口返回异常");
                return response;

            }

        } catch (Exception e) {
            SyncPackagesResponse response = new SyncPackagesResponse();
            response.setRetCode("9999");
            response.setResultInfo("接口出现异常");
            return response;
        }

    }

    /**
     * 3.24	批量同步设备可订购套餐
     *
     * @author lm
     * @date 2019/8/29
     */
    public SyncPackageAndDeviceResponse syncDevicePackages(Integer pageNo, Integer pageSize, String partnerCode) {
        try {
            SyncPackageAndDeviceRequestBody body = new SyncPackageAndDeviceRequestBody();
            body.setPartnerCode(partnerCode);
            if (pageNo != null) {
                body.setPageNo(pageNo.toString());
            } else {
                body.setPageNo("1");
            }
            if (pageNo != null) {
                body.setPageSize(pageSize.toString());
            } else {
                body.setPageSize("20");
            }

            // 请求参数拼接
            SyncPackageAndDeviceRequest request = new SyncPackageAndDeviceRequest();
            TurntoInterfaceUtils utils = new TurntoInterfaceUtils();

            RequestHeader header = utils.getRequestHeader(Constant.cmd_syncPackageAndDevice, Constant.cmd_accessCode);
            request.setHeader(header);
            request.setBody(body);

            String sign = utils.getSign(header, body, body.getClass(), Constant.cmd_secret);
            request.setSign(sign);

            // 将参数转换成XML格式
            String sRequest = new JaxbUtil(SyncPackageAndDeviceRequest.class).toXml(request, "utf-8");
            logger.info("【批量同步设备可订购套餐】xml请求：" + sRequest);
            // 发送post请求，并接收返回结果
            String result = HttpClientTool.sendHttpPost(Constant.BASE_URL, sRequest);
            logger.info("URL=" + Constant.BASE_URL + ";接口名：" + Constant.cmd_syncPackageAndDevice + ";请求的企业：" + partnerCode);

            logger.info("【批量同步设备可订购套餐】xml响应：" + result);
            // 将回传的xml转换成object数据
            SyncPackageAndDeviceResponse responseObj = new JaxbUtil(SyncPackageAndDeviceResponse.class).fromXml(result, "utf-8");

            if (result != null && !result.equals("-1")) {

                responseObj.setBody(responseObj.getBody());
                responseObj.setRetCode(responseObj.getHeader().getResultCode());
                responseObj.setResultInfo(responseObj.getHeader().getResultInfo());
                return responseObj;

            } else {

                SyncPackageAndDeviceResponse response = new SyncPackageAndDeviceResponse();
                response.setRetCode("9998");
                response.setResultInfo("接口返回异常");
                return response;

            }

        } catch (Exception e) {

            SyncPackageAndDeviceResponse response = new SyncPackageAndDeviceResponse();
            response.setRetCode("9999");
            response.setResultInfo("接口出现异常");
            return response;

        }
    }
}